package com.cxhome.cxinfox.productvas;

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.traffic.managent.base.BaseService;
import com.traffic.managent.base.Paginator;
import com.traffic.managent.model.product.Product;
import com.traffic.managent.util.SQLUtils;
import com.traffic.managent.util.UtilsUtils;

/**
 * 平台产品业务
 * 
 */
public class ProductService extends BaseService {

	private final static Logger log = Logger.getLogger(ProductService.class);
	public static final ProductService service = new ProductService();
	
	public Page<Product> paginate(Product model, Paginator paginate, StringBuilder orderSql) {

		String select = "SELECT ss.*, REPLACE( (  SELECT  group_concat(  concat(  cp.providerName, "
				+ " '----',  cpi.productName ,'----',cpv.level )  )  FROM  cx_product_vasproduct cpv "
				+ " LEFT JOIN cx_product_info cpi ON cpv.pid = cpi.ids "
				+ " LEFT JOIN cx_provider cp ON cpi.providerId = cp.ids  WHERE  cpv.vasPid = ss.ids "
				+ " AND cpv.`status` = '0'  ),',','<br /><br />') AS deploys, (  SELECT  COUNT(1)  FROM "
				+ " cx_product_vasproduct cpv  LEFT JOIN cx_product_info cpi ON cpv.pid = cpi.ids "
				+ " LEFT JOIN cx_provider cp ON cpi.providerId = cp.ids  WHERE  cpv.vasPid = ss.ids "
				+ " AND cpv.`status` = '0'  ) AS deployCount ";

		SQLUtils sql = new SQLUtils(" FROM  cx_product_vas ss  WHERE ss.`status` = '0' ");

		if (model.getAttrValues().length != 0) {
			sql.whereLike("ss.ids", model.getStr("ids"));
			sql.whereLike("ss.vasProdName", model.getStr("vasProdName"));
		}
		sql.append(orderSql);
		return Product.dao.paginate(paginate, select, sql.toString());
	}

	
	public void saveOrUpdate(String ids, Product model, String userid) {
		if (!"".equals(ids) && ids != null) { // 更新
			model.put("updateName", userid);
			model.put("updateTime", new Date());
			model.update();
		} else { // 新增
			model.put("createName", userid);
			model.put("createTime", new Date());
			model.save();
		}

	}
	
	/**
	 * 供应商产品ids，本地产品id
	 * @param productinfoids
	 * @param ids
	 */
	@Before(Tx.class)
	public void saveBind(String productinfoids, String ids) {
		Db.update("update cx_product_vasproduct set status = '1' where vasPid = ?", ids);
		String[] productinfoidsArrs = productinfoids.split(",");
		for (int i = 0; i < productinfoidsArrs.length; i++) {
			
			String[] pidlevelArr = productinfoidsArrs[i].split("-");
			ProductVasproduct pvp = new ProductVasproduct();
			String productinfoid = pidlevelArr[0];
			String level = pidlevelArr[1];
			
			pvp.set("ids", UtilsUtils.getRandomId(30));
			pvp.set("vasPid", ids);
			pvp.set("pid", productinfoid);
			pvp.set("level", level);
			pvp.set("status", "0");
			pvp.save();
		}

	}
	
	/**
	 * 条件查询
	 * @return
	 */
	public List<Product> findByParam(String productName) {

		String sql = "select * from cx_product_vas where 1=1 and status = '0' ";
		if (productName != null) {
			sql += " and vasProdName like '%" + productName + "%'";
		}
		return Product.dao.find(sql, productName);
	}
}
